<?php

namespace App\Http\Controllers;

use App\Models\Members;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Collection;
use Spatie\Fractal\Fractal;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function getResponse( $msg = '', $data = [], $code = 200, $redirect = 'current'){

        if (is_array($data) && count($data) > 0) {
            array_walk_recursive($data, function (&$item) {
                if (is_null($item)) {
                    $item = '';
                }
            });
        }

        return response()->json([
            'code'      => $code,
            'message'   => $msg,
            'data'      => $data,
            'redirect'  => $redirect
        ]);

    }

    public function getResponseSuccess($msg,$redirect='current'){
        return $this->getResponse($msg,[],200,$redirect);
    }

    public function ajaxResponse($code = 200000, $data = [], $msg = '')
    {
        $statusCode = 200;
        $_data['code'] = strval($code);
        $_data['message'] = $msg;
        if (is_array($data) && count($data) > 0) {
            array_walk_recursive($data, function (&$item) {
                if (is_null($item)) {
                    $item = '';
                }
            });
        }else if($data instanceof Collection || $data instanceof Fractal){
            $data = $data->toArray();
            array_walk_recursive($data, function (&$item) {
                if (is_null($item)) {
                    $item = '';
                }
            });
        }

        if (!empty($data)) {
            $_data['data'] = $data;
        }else{
            $_data['data'] = (object)[];
        }
        return response()->json($_data, $statusCode, []);
    }

    public function ajaxSuccess($data = [], $message = '')
    {
        return $this->ajaxResponse(200000, $data, $message);
    }

    public function ajaxResponseV1($code = 200000, $data = [], $msg = '')
    {
        $statusCode = 200;
        $_data['code'] = strval($code);
        $_data['info'] = $msg;
        if (is_array($data) && count($data) > 0) {
            array_walk_recursive($data, function (&$item) {
                if (is_null($item)) {
                    $item = '';
                }
            });
        }else if($data instanceof Collection || $data instanceof Fractal){
            $data = $data->toArray();
            array_walk_recursive($data, function (&$item) {
                if (is_null($item)) {
                    $item = '';
                }
            });
        }

        if (!empty($data)) {
            $_data['data']['Info'] = $data;
        }else{
            $_data['data']['Info'] = (object)[];
        }
        return response()->json($_data, $statusCode, []);
    }

    public function getUserID()
    {
        return getUserID();
    }

    public function getUserInfo()
    {

        $user = Members::whereId($this->getUserID())->first();
        return $user;
    }

    /**
     * @param string $arrData 数组
     * @return int 1 成功 0 失败
     * array:2 [
     * "error" => "失败：请检查接口是否畅通！"
     * "code" => "20000"
     * ]
     */
    public function getCodeResponseState($arrData = '')
    {
        if (empty($arrData)) {
            $state = 0;
            $msg = '数据不能为空';
        } else {
            $code = $arrData['code'];
            $msg = $arrData['error'];
            switch ($code) {
                case '20000':
                    $state = 0;
                    break;
                case '10000':
                    $state = 1;
                    break;
                default:
                    $state = 0;
            }
        }
        return $state;
    }
}
